package com.mvvm.demo.ui.login

import androidx.lifecycle.*
import com.mvvm.base.base.ExceptionHandle
import com.mvvm.base.base.viewmodel.BaseViewModel
import com.mvvm.base.ext.postNext
import kotlinx.coroutines.launch
import java.lang.Exception

@SuppressWarnings("checkResult")
class LoginViewModel(private val repo: LoginRepository) : BaseViewModel() {
    private val _stateLiveData: MutableLiveData<LoginViewState> =
            MutableLiveData(LoginViewState.initial())

    val stateLiveData: LiveData<LoginViewState> = _stateLiveData

    init {
        viewModelScope.launch {
            _stateLiveData.postNext { state ->
                state.copy(
                        isLoading = false,
                        error = null,
                        userInfo = null
                )
            }
        }
    }

    fun getVerficationCode(phone: String) {
        when (phone.isNullOrEmpty()) {
            true -> _stateLiveData.postNext { state ->
                state.copy(isLoading = false, error = "验证码为空", userInfo = null)
            }
            false -> viewModelScope.launch {
                _stateLiveData.postNext {
                    it.copy(isLoading = true, error = null, userInfo = null)
                }

                try {
                    val result = repo.getVerficationCode(phone)
                    if (result.code == 200) {
                        _stateLiveData.postNext {
                            it.copy(isLoading = false, error = "验证码发送成功", userInfo = null)
                        }
                    } else {
                        _stateLiveData.postNext {
                            it.copy(isLoading = false, error = "验证码发送失败", userInfo = null)
                        }
                    }
                } catch (e: Exception) {
                    _stateLiveData.postNext {
                        it.copy(isLoading = false, error = ExceptionHandle.handleException(e), userInfo = null)
                    }
                }


            }
        }
    }

    fun login(phone: String, code: String) {
        when (phone.isNullOrEmpty() || code.isNullOrEmpty()) {
            true -> _stateLiveData.postNext { state ->
                state.copy(isLoading = false, error = "手机号或验证码为空", userInfo = null)
            }
            false -> viewModelScope.launch {
                _stateLiveData.postNext {
                    it.copy(isLoading = true, error = null, userInfo = null)
                }
                try {
                    val result = repo.login(phone, code)
                    repo.insertUser(result.data)
                    if (result.code == 200) {
                        _stateLiveData.postNext {
                            it.copy(isLoading = false, error = result.msg, userInfo = result.data)
                        }
                    } else {
                        _stateLiveData.postNext {
                            it.copy(isLoading = false, error = result.msg, userInfo = null)
                        }
                    }
                } catch (e: Exception) {
                    _stateLiveData.postNext {
                        it.copy(isLoading = false, error = ExceptionHandle.handleException(e), userInfo = null)
                    }
                }
            }
        }

    }


}

@Suppress("UNCHECKED_CAST")
class LoginViewModelFactory(
        private val repo: LoginRepository
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T =
            LoginViewModel(repo) as T
}